# Pyston 0.01

# Global imports
import pygame, os, sys, string
from pygame.locals import *
from pygame.time import *

# Local imports
from pyston_config import *
from globals import *
from ResourceManager import *
from LogicalEntity import *
from DynamicEntity import *
from EntityManager import *

# Global class
class Pyston:
    
    # Constructor
    def __init__(self):
        # Initialise pygame
        pygame.init()
        self.screen = pygame.display.set_mode((screen_width, screen_height))
        
        # Caption
        pygame.display.set_caption(game_name + " (" + game_version + ") by " + game_author)
        
        # Initialise the various management systems
        # Resource manager
        self.resourceManager = ResourceManager()
        self.entityManager = EntityManager(self.resourceManager)
        
        # Setup the background surface which we will be rendering everything on to
        self.background = pygame.Surface((screen_width, screen_height))
        self.background = self.background.convert()
        self.background.fill(screen_color)
        
        # Initially set the event queue
        pygame.key.set_repeat(1, 10)
        self.events = pygame.event.get()
        
        # Timing
        self.timeScale = 1.000
        self.frameDelta = 0.000
        self.currentFrameTime = 0
        self.lastFrameTime = pygame.time.get_ticks()
        self.timer = pygame.time.Clock()
    
    # Main run loop
    def stepFrame(self):     
        # Timing
        self.currentFrameTime = pygame.time.get_ticks()
        self.frameDelta = (self.currentFrameTime - self.lastFrameTime) / 1000.000
        self.lastFrameTime = self.currentFrameTime
        
        # Step through each object using time delta
        self.entityManager.stepFrame(self.frameDelta * self.timeScale)

        # Draw the frame
        self.screen.blit(self.background, (0, 0))
        
        # Draw the sprites
        self.entityManager.draw(self.screen)
        pygame.display.flip()
                    
        # Keep a constant FPS
        self.timer.tick(screen_fps)
    
    # Processing input
    def gatherEvents(self):
        self.events = pygame.event.get()
        
    # Setting time scale
    def setTimeScale(self, newTimeScale = 1.000):
        self.timeScale = newTimeScale
        
    def getTimeScale(self):
        return self.timeScale